package com.ec.cr.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ec.api.domain.po.MedicineRecord;
import com.ec.cr.domain.vo.MedicineRecordVO;
import com.ec.cr.domain.vo.StatItemVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Mapper
public interface MedicineRecordMapper extends BaseMapper<MedicineRecord> {
    @Select("SELECT mr.id, mr.elderly_user_id, mr.drug_id, di.name AS drug_name, mr.dosage, mr.frequency, " +
            "mr.start_date, mr.end_date, mr.cost, mr.created_at " +
            "FROM medicine_record mr " +
            "LEFT JOIN drug_info di ON mr.drug_id = di.id " +
            "WHERE mr.elderly_user_id = #{userId} " +
            "AND mr.start_date >= #{startDate} " +
            "AND mr.end_date <= #{endDate}")
    List<MedicineRecord> selectRecordsWithDrugName(
            @Param("userId") Long userId,
            @Param("startDate") LocalDate startDate,
            @Param("endDate") LocalDate endDate
    );


    List<MedicineRecordVO> findRecords(@Param("elderlyName") String elderlyName, @Param("drugName") String drugName);


    List<MedicineRecordVO> selectPageRecords(String elderlyName, String drugName, int offset, int pageSize);

    long countRecords(String elderlyName, String drugName);

    List<Map<String, Object>> selectElderlySimpleList();

    void insertRecord(MedicineRecord record);

    void updateRecord(MedicineRecord record);

    void deleteRecord(Integer id);

    List<StatItemVO> countByMonth(Long elderlyUserId);

    List<StatItemVO> sumByDrug(Long elderlyUserId);

    List<StatItemVO> sumCostByDay(Long elderlyUserId);

    List<MedicineRecordVO> selectPageRecordsByUserId(Long elderlyUserId, String drugName, int offset, int pageSize);

    long countRecordsByUserId(Long elderlyUserId, String drugName);
}
